package com.lanyam.travel.base.controller;


import com.lanyam.travel.base.entity.excelEntity.TravelOrderDetailsForExcel;
import com.lanyam.travel.base.service.IExcelService;
import com.lanyam.travel.base.utils.JSONResult;
import com.lanyam.travel.base.entity.TravelOrder;
import com.lanyam.travel.base.entity.vo.TravelOrderVo;
import com.lanyam.travel.base.service.ITravelOrderService;
import com.lanyam.travel.base.utils.JsonMapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.core.type.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/travelOrder")
public class TravelOrderController {

    @Autowired
    private ITravelOrderService travelOrderService;
    @Autowired
    private IExcelService excelService;

    /**
     * 分页查询数据
     */
    @GetMapping("/getPage")
    public IPage<TravelOrderVo> getPage (String queryMap,
                                                @RequestParam(name="page", defaultValue = "1") Integer page,
                                                @RequestParam(name="rows", defaultValue = "10") Integer rows) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        query = query==null?new HashMap<>():query;
        IPage<TravelOrderVo> pageInfo = travelOrderService.getPageTravelOrder(query, page, rows);
        return pageInfo;
    }

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public JSONResult add (@RequestBody TravelOrder travelOrder) {
        JSONResult result = null;
        try{
            travelOrderService.addTravelOrder(travelOrder);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 编辑数据
     */
    @PostMapping("/update")
    public JSONResult update (@RequestBody TravelOrder travelOrder) {
        JSONResult result = null;
        try{
            travelOrderService.updateOrder(travelOrder);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 删除数据
     */
    @PostMapping("/delById")
    public JSONResult delById (String id) {
        JSONResult result = null;
        try {
            travelOrderService.delById(id);
            result = JSONResult.ok();
        }  catch (Exception e) {
            e.printStackTrace();
            String msg = e.getMessage();
            result = JSONResult.errorMsg(msg);
        }
        return result;
    }

    /**
     * 导出excel数据
     */
    @GetMapping("/exportExcel")
    public void exportExcel(String queryMap, HttpServletResponse response) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        try {
            travelOrderService.exportExcel(query, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出酒店日报
     * @param response
     */
    @GetMapping(value="/exportHotel")
    public void exportHotel(HttpServletResponse response, String queryMap) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        query = query==null?new HashMap<>():query;
        try {
            excelService.exportHotelGoods(response, query);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出景点日报
     * @param response
     */
    @GetMapping(value="/exportTicket")
    public void exportTicket(HttpServletResponse response, String queryMap) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        query = query==null?new HashMap<>():query;
        try {
            excelService.exportTicketGoods(response, query);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出线路日报
     * @param response
     */
    @GetMapping(value="/exportLine")
    public void exportLine(HttpServletResponse response, String queryMap) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        query = query==null?new HashMap<>():query;
        try {
            excelService.exportLineGoods(response, query);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * excel分页查询数据
     */
    @GetMapping("/getExcelPage")
    public IPage<TravelOrderDetailsForExcel> getExcelPage (String queryMap,
                                                           @RequestParam(name="page", defaultValue = "1") Integer page,
                                                           @RequestParam(name="rows", defaultValue = "10") Integer rows) {
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        query = query==null?new HashMap<>():query;
        IPage<TravelOrderDetailsForExcel> pageInfo = excelService.getExcelPage(query, page, rows);
        return pageInfo;
    }

    /**
     * 分页查询数据
     */
    @GetMapping("/getDistrOrderList")
    public JSONResult queryOrderByPage(String queryMap,
                                       @RequestParam(name="page", defaultValue = "1") Integer page,
                                       @RequestParam(name="rows", defaultValue = "10") Integer rows) {
        JSONResult result = null;
        Map<String, Object> query = JsonMapper.string2Obj(queryMap,new TypeReference<Map<String, Object>>() {});
        query = query==null?new HashMap<>():query;
        IPage<TravelOrderVo> pageInfo = null;
        try {
            pageInfo = travelOrderService.getDistrOrderList(page, rows, query);
            result = JSONResult.ok(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}